import json

from mitmproxy import http, ctx
from mitmproxy.tools.main import mitmdump


class Events:

    def response(self, flow: http.HTTPFlow):

        if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" in flow.request.pretty_url\
                and 'x=' in flow.request.pretty_url:

            ctx.log.info(flow.response.text)

            # 获取响应信息
            resp = json.loads(flow.response.text)

            # 修改响应体的部分值
            resp['data']['items'][0]['quote']['name'] = '边界值：-0.00001'
            resp['data']['items'][0]['quote']['percent'] = -0.00001

            resp['data']['items'][1]['quote']['name'] = '边界值：0.00000'
            resp['data']['items'][1]['quote']['percent'] = 0.00000

            resp['data']['items'][2]['quote']['name'] = '边界值：+0.00001'
            resp['data']['items'][2]['quote']['percent'] = 0.00001

            # 返回给客户端
            flow.response.text = json.dumps(resp)



addons = [
    Events()
]


if __name__ == '__main__':
    mitmdump(['-p', '8080', '-s', __file__])
